package com.zkingsoft.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.BizActivities;
import com.zkingsoft.bean.BizCollage;
import com.zkingsoft.bean.BizCollageDetail;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.common.constance.AppMessageCode;
import com.zkingsoft.dao.BizActivitiesDao;
import com.zkingsoft.dao.BizCollageDao;
import com.zkingsoft.dao.BizCollageDetailDao;
import com.zkingsoft.service.BizCollageService;

/**
 * @description service接口实现类(拼团)
 * @author 何明
 * @date 2018-05-23 00:08
 */
@Service
public class  BizCollageServiceImpl implements  BizCollageService{

	
	@Autowired
	private BizCollageDao bizCollageDao;
	@Autowired
	private BizActivitiesDao bizActivitiesDao;
	@Autowired
	private BizCollageDetailDao bizCollageDetailDao;
	@Override
	public int add(BizCollage bizCollage){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizCollage.setCreateBy(loginUser.getSuName());
		bizCollage.setUpdateBy(loginUser.getSuName());
		bizCollage.setColId(UUIDUtil.getRandomID());
		return bizCollageDao.insert(bizCollage);
		
	}
	
	@Override
	public int batchAdd(List<BizCollage>  bizCollageList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizCollageList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizCollageList.size() ? bizCollageList.size() : end;
			List<BizCollage> insertList = bizCollageList.subList(begin, end);
			num += bizCollageDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizCollage oldBizCollage
	,BizCollage newBizCollage){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizCollage, newBizCollage)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizCollage, newBizCollage);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizCollage);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("colId", oldBizCollage.getColId());
			bizCollageDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizCollage bizCollage){
	
		return bizCollageDao.updateByModel(bizCollage);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizCollageDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String colId){
	
		return bizCollageDao.deleteById(colId);
	
	}
	
	@Override
	public int removeByModel(BizCollage bizCollage){
	
		return bizCollageDao.deleteByModel(bizCollage);
	
	}
	
	
	@Override
	public List<BizCollage> findInPage(BizCollage bizCollage,  PaginationVO pageVo){
	
		return bizCollageDao.selectInPage(bizCollage , pageVo);
	
	}
	
	@Override
	public List<BizCollage> findByModel(BizCollage bizCollage){
	
		return bizCollageDao.selectByModel(bizCollage);
	
	}
	
	@Override
	public int  findTotal(BizCollage bizCollage){
	
		return bizCollageDao.selectTotalRecord(bizCollage);
	
	}
	
	@Override
	public BizCollage  findById(String colId){
	
		return bizCollageDao.selectById(colId);
	
	}

	/**
	 * 新增拼团活动
	 */
	@Override
	@Transactional
	public int addCollage(BizActivities bizActivities,BizUser loginUser) {
		// 设置活动信息
				bizActivities.setCreateBy(loginUser.getOpenId());
				bizActivities.setUpdateBy(loginUser.getOpenId());
				bizActivities.setActId(UUIDUtil.getRandomID());
				bizActivities.setUserId(loginUser.getOpenId());
				bizActivities.setActType(AppConstance.ACTIVITIES_COLLAGE);
				int i=bizActivitiesDao.insert(bizActivities);
				if(i>0){
					// 设置拼团信息	
					BizCollage bizCollage=bizActivities.getCollage();
					bizCollage.setActId(bizActivities.getActId());
					bizCollage.setCreateBy(loginUser.getOpenId());
					bizCollage.setUpdateBy(loginUser.getOpenId());
					bizCollage.setColId(UUIDUtil.getRandomID());
					i=bizCollageDao.insert(bizCollage);
				}
		
				return i;
		
	}

	/**
	 * 修改拼团活动
	 * 已经开团的能修改：拼团信息
	 */
	@Override
	@Transactional
	public int modifyCollage(BizActivities bizActivities, BizUser loginUser) {
		//修改活动信息
		bizActivities.setUpdateBy(loginUser.getOpenId());
		bizActivities.setUserId(loginUser.getOpenId());
		bizActivities.setUpdateTime(new Date());
		int i=bizActivitiesDao.updateByModel(bizActivities);
		if(i>0){
			//查询是否有开团信息
			BizCollageDetail bizCollageDetail=new BizCollageDetail();
			bizCollageDetail.setActId(bizActivities.getActId());
			List<BizCollageDetail> detailList=
			bizCollageDetailDao.selectByModel(bizCollageDetail);
			if(CollectionUtils.isEmpty(detailList)){
				//修改拼团信息
				BizCollage bizCollage=bizActivities.getCollage();
				bizCollage.setUpdateBy(loginUser.getOpenId());
				bizCollage.setUpdateTime(new Date());
				i=bizCollageDao.updateByModel(bizCollage);
			}
		}
		return i;
		
	}
	/**
	 * 删除拼团活动
	 * 已经开团的活动不能删除
	 */
	@Override
	@Transactional
	public int delCollage(List<String> ids, BizUser loginUser) {
		for(String actId:ids){
			BizActivities bizActivities=new BizActivities();
			bizActivities.setActId(actId);
			//查询是否有开团信息
			BizCollageDetail bizCollageDetail=new BizCollageDetail();
			bizCollageDetail.setActId(bizActivities.getActId());
			List<BizCollageDetail> detailList=
			bizCollageDetailDao.selectByModel(bizCollageDetail);
			if(CollectionUtils.isEmpty(detailList)){
				//无开团信息,修改活动状态为删除
				bizActivities.setUserId(loginUser.getOpenId());
				bizActivities.setUpdateBy(loginUser.getOpenId());
				bizActivities.setUpdateTime(new Date());
				bizActivities.setActStatus(AppConstance.ACTIVITIES_STATUS_DEL);
				bizActivitiesDao.updateByModel(bizActivities);
			}else{
				LogUtil.debug("此活动已开团不能删除：{}", actId);
				throw new GlobleException(AppMessageCode.Collage.COLLAGE_DEL_ERR);
			}
		}
		
		return 1;
	}
	
	
	
	
}